package com.cheng.xbsc.controller;

import com.cheng.xbsc.entity.Address;
import com.cheng.xbsc.entity.vo.Result;
import com.cheng.xbsc.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 查询用户的所有收货地址
     *
     * @return
     */
    @GetMapping()
    public Result findAll() {
        List<Address> addressList = addressService.findByUSerId();
        return new Result(true, "查询成功", addressList);
    }

    /**
     * 添加收货地址
     *
     * @param address
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody Address address) {
        addressService.save(address);
        return new Result(true, "添加成功");
    }

    /**
     * 设置默认地址
     * @param aId
     * @return
     */
    @PutMapping("/updateStatus/{aId}")
    public Result updateStatus(@PathVariable("aId") Integer aId) {
        addressService.updateStatus(aId);
        return new Result(true, "设置成功");
    }

    /**
     * 删除收货地址
     * @param aId
     * @return
     */
    @DeleteMapping("/delete/{aId}")
    public Result delete(@PathVariable("aId") Integer aId) {
        addressService.deleteById(aId);
        return new Result(true, "删除成功");
    }
}
